var form;
var laydate;

$(document).ready(function () {
    layui.use(['form', 'laydate'], function () {
        var form = layui.form;
        form.render();

        initDater();
        getWages();

        // 监听Select
        form.on("select(selectFilter)", function(data) {
            $("input[name='householdId']").val(data.value);
        });

    });
});

function initDater() {
    laydate.render({
        elem: '#wagesYearMonthEdit'
        ,type: 'month'
    });
}

function getWages() {

    var id = $('#id').val();

    ajax(
        "/admin/wages/getWages", "POST",
        { wagesId: id }
        ,true, true, false
    ).done(function (returnData) {

        $("input[name='wagesId']").val(returnData.wagesId);
        $("input[name='wagesYearMonth']").val(returnData.wagesYearMonth);
        $("input[name='baseWages']").val(returnData.baseWages);
        $("input[name='bonusMoney']").val(returnData.bonusMoney);
        $("input[name='fullWorkMoney']").val(returnData.fullWorkMoney);
        $("input[name='overtimeMoney']").val(returnData.overtimeMoney);
        $("input[name='laterMoney']").val(returnData.laterMoney);
        $("input[name='absenteeismMoney']").val(returnData.absenteeismMoney);
        $("input[name='leaveMoney']").val(returnData.leaveMoney);
        $("input[name='householdId']").val(returnData.householdId);
        $("input[name='householdName']").val(returnData.householdName);
        $("input[name='householdNo']").val(returnData.householdNo);
        var select = $("#wagesHouseholdName");

        if (returnData.householdName != null) {
            select.empty();
            select.append("<option value=''>搜索工资</option>");
            select.append("<option value='" + returnData.householdId + "' selected>" + returnData.householdName + "</option>");
        }

        form.render();
        // 监听输入框
        reloadSelectListener();

    });

}

function editWages() {

    var formJSON = $("#wages-submit-form").serializeObject();

    // 编辑工资
    ajax(
        "/admin/wages/editWages", "POST",
        formJSON, true, true, true
    ).done(function () {
        loadingPage("/admin/wages/wages");
    });

}


function onSelectChange() {
    var householdName = $("#houseHouseholdDiv>div>div>input").val();
    ajax(
        "/admin/wages/getPersonnel", "POST",
        { householdName: householdName }
        ,false, true, false
    ).done(function (returnData) {
        if (returnData.length > 0) {

            var select = $("#wagesHouseholdName");
            select.empty();
            select.append("<option value=''>搜索员工</option>");

            for (var i = 0; i < returnData.length; i ++) {
                select.append("<option value='" + returnData[i].householdId + "'>" + returnData[i].householdName + "(" + returnData[i].householdNo + ")</option>");
            }

            form.render('select');
            reloadSelectListener();
        }


    });
}

function reloadSelectListener() {

    // 监听输入框
    $("#houseHouseholdDiv>div>div>input").bind("input propertychange", function() {
        onSelectChange();
    });

}

